package part8;

import java.util.Random;

/**
 * @author wcj
 * @Date 2021/5/16 16:45
 * @Version 1.0
 */
public class Part82 {

    private static ShapeGenerate shapeGenerate = new ShapeGenerate();

    public static void main(String[] args) {
        Shape[] s = new Shape[9];
        for (int i = 0; i < s.length; i++) {
            s[i] = shapeGenerate.next();
        }
        // 注意:多态的实现依赖于后期绑定
        // 前期绑定：即程序运行之前就知道是什么类型
        // 后期绑定：程序运行期间才知道具体的类型，多态即是依赖于这种特性
        for (Shape shape : s) {
            shape.draw();
        }
    }

}

class ShapeGenerate {
    private Random random = new Random(47);

    public Shape next() {
        switch (random.nextInt(3)) {
            default:
            case 1:
                return new Circle();
            case 2:
                return new Square();
            case 0:
                return new Triangle();
        }
    }
}

class Shape {
    public void draw() {
        System.out.println("shape draw");
    }

    public void erase() {
        System.out.println("shape erase");
    }
}

class Circle extends Shape {
    @Override
    public void draw() {
        System.out.println("Circle draw");
    }

    @Override
    public void erase() {
        System.out.println("Circle erase");
    }
}

class Square extends Shape {
    @Override
    public void draw() {
        System.out.println("Square draw");
    }

    @Override
    public void erase() {
        System.out.println("Square erase");
    }
}

class Triangle extends Shape {
    @Override
    public void draw() {
        System.out.println("Triangle draw");
    }

    @Override
    public void erase() {
        System.out.println("Triangle erase");
    }
}
